# coding:utf-8
import hashlib
from strategy.models import Strategy, CreditGlobalAction


# 获取到文件的md5值，传入的为二进制格式
def file_md5(file_binary_data):
    md5 = hashlib.md5()
    md5.update(file_binary_data)
    return md5.hexdigest()


def get_uid_info():
    uid_channel_dict = {}
    uid_name_dict = {}
    strategy_queryset = Strategy.objects.all()
    for strategy in strategy_queryset:
        uid_channel_dict[strategy.uid] = strategy.channel
        uid_name_dict[strategy.uid] = strategy.name
    return uid_channel_dict, uid_name_dict


def get_risk_level(safety_score):
    unique_score_list = CreditGlobalAction.objects.order_by('score_upper').values_list('score_upper', flat=True).distinct()
    if safety_score < unique_score_list[0]:
        return "高风险用户"
    elif unique_score_list[0] <= safety_score < unique_score_list[1]:
        return "中风险用户"
    elif unique_score_list[1] <= safety_score < unique_score_list[2]:
        return "低风险用户"
    else:
        return "安全用户"


def get_name_uid(name):
    """获取name相同策略的uid
    """
    uid_list = []
    instance = Strategy.objects.filter(name=name).all()
    if instance:
        for instance_i in instance:
            uid_list.append(instance_i.uid)

    return uid_list
